define([
    "jquery",
    "./scheduleCon.js",
    "./lang_en.js","./lang_zh.js"
], function ($,scheduleCon,en,zh) {
    var load = function (bajaObj,globalColor) {
        var msg = JSON.parse(bajaObj.getJsonData());
        if("language" in msg.item){
            if(msg.item["language"]=="zh"){
                var languageData = zh.languageData;
            }else if(msg.item["language"]=="en"){
                var languageData = en.languageData;
            }
        }else{
            var languageData = zh.languageData;
        }
        var scheduleOrd = "";
        getSpiceData();        
        function getSpiceData() {
            bajaObj.ajax({
                showType: "ReadSpecial"
            }, SpecialajaxSuccess, SpecialajaxError);
        }
        function SpecialajaxSuccess(msg) {
            scheduleOrd = msg.item.scheduleOrd;
            scheduleCon.load2(bajaObj, msg, globalColor);
            getWeekData();
        }
        function SpecialajaxError(msg){
            console.log(msg);
        }
        function getWeekData(){
            bajaObj.ajax({
                item: {
                    background: null,
                    scheduleOrd: scheduleOrd,
                    type: "readWeeklySchedule"
                },
                showType: "RefreshWeekly"
            }, weekajaxSuccess, weekajaxError);
        }
        function weekajaxSuccess(msg){
            msg.item.weekPlans.map((item,i)=>{
                if(item.events.length!=0){
                    setDaySelect(i);
                }
            })
        }
        function weekajaxError(msg){
            console.log(msg);
        }
        function setDaySelect(day) {
            // console.log("需要选中的星期",day);
            if (day == 7) {
                day = 0;
            }
            $(".dateContent").find(".day" + day).addClass("selectedDate");
        }
        
        $("#overviewSchedule").unbind("click").on("click",".date",function(){
            var classNameList = $(this).attr("class").split(" ")[0];
            // console.log(classNameList);
            var yearCom = classNameList.slice(4,8);
            var monthCom = classNameList.slice(8,10);
            var dateCom = classNameList.slice(10,12);
            defaultDate = yearCom + "-" + monthCom + "-" + dateCom;
            // console.log(yearCom,monthCom,dateCom,defaultDate);
            $(".weekContent .today").removeClass("today");
            $(this).addClass("today");
            getSummaryData(defaultDate);
        })
        // 获取数据
        function getSummaryData(time){
            bajaObj.ajax({
                showType: "ReadSummary",
                time:time
            }, SummaryajaxSuccess, SummaryajaxError);
        }
        function SummaryajaxSuccess (reData){
            // console.log(reData);
            var str = "";
            for(var i=0;i<reData.item.summaries.length;i++){
                var nowData = reData.item.summaries[i];
                var type = "";
                if(nowData.source.indexOf("Default")!=-1){
                    type = languageData.defaultOutput;
                }else if(nowData.source.indexOf("Week")!=-1){
                    type = languageData.week + " " + nowData.text;
                }else if(nowData.source.indexOf("Special")!=-1){
                    type = languageData.specialSchedule + nowData.text;
                }
                str += `<dd>
                            <ul class="ptop">
                                <li class='ball'>
                                    <span></span>
                                </li>
                                <li class="time">
                                    <span class="iconfont icon-time"></span>
                                    <span class="timeText">${languageData.time}</span>
                                    <span class="timeCont">${nowData.timestamp}</span>
                                </li>
                                <li class="type">
                                    <span class="iconfont icon-liuchengshijian"></span>
                                    <span class="timeText">${languageData.eventType}</span>
                                    <span class="timeCont">${type}</span>
                                </li>
                            </ul>
                            <ul class="eventUl">
                                <li class="time eventText">
                                    <span class="iconfont icon-guanlian"></span>
                                    <span class="timeText">${languageData.eventSource}</span>
                                    <span class="timeCont">${nowData.output}</span>
                                </li>
                            </ul>
                        </dd>`;
            }
            $("#overviewSchedule").find(".specialList div").empty().append(str);
        }
        function SummaryajaxError(err){
            console.log(err);
        }
        function todayClick(){
            var now = new Date();
            var year = now.getFullYear();
            var month = now.getMonth()+1 > 9 ? now.getMonth()+1 : "0" + (now.getMonth()+1);
            var date = now.getDate() > 9 ? now.getDate() : "0" + now.getDate();
            var defaultDate = year + "-" + month + "-" + date;
            getSummaryData(defaultDate);
        }
        todayClick();
        $("#overviewSchedule .reloadSpiceButton").unbind("click").click(function(){
            getSpiceData();
            todayClick();
        })
        
        $("#overviewSchedule").on("click",".jumpToday",function(){
            todayClick();
        })
    };
    return {
        load:load,
    }
});


